package cn.eud.hunau.service.impl;

import cn.eud.hunau.exception.BusinessException;
import cn.eud.hunau.mapper.UserMapper;
import cn.eud.hunau.pojo.User;
import cn.eud.hunau.request.UserReq;
import cn.eud.hunau.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired// 默认按类型
    private UserMapper userMapper;

    @Override
    public User login(UserReq reqObject) {

      User user=  userMapper.queryByUserNameAndPassword(reqObject.getUsername(),reqObject.getPassword());

      // 判断用户是否存在
      if(user==null){
          throw new BusinessException("用户名或密码错误",400401);
      }

      return user;
    }

    @Override
    public boolean register(UserReq reqObject) {
        // 检查用户名是否已存在
        User exist = userMapper.queryByUserNameAndPassword(reqObject.getUsername(), reqObject.getPassword());
        if (exist != null) {
            throw new BusinessException("用户名已存在", 500100);
        }

        // 生成用户
        User user = new User();
        user.setUsername(reqObject.getUsername());
        user.setPassword(reqObject.getPassword());

        return userMapper.insertUser(user) > 0;
    }
}
